package com.elong.businesstravel.modules.home.hotel.hotelpayment;

import android.content.Intent;
import android.graphics.Bitmap;
import android.os.Bundle;
import android.view.KeyEvent;
import android.view.View;
import android.view.View.OnClickListener;
import android.webkit.WebSettings;
import android.webkit.WebView;
import android.webkit.WebViewClient;
import android.webkit.WebSettings.LayoutAlgorithm;

import com.android.support.jhf.debug.DebugLog;
import com.elong.businesstravel.R;
import com.elong.businesstravel.base.baseactivity.BaseActivity;
import com.elong.businesstravel.base.baseactivity.BaseWebViewActivity;
import com.elong.businesstravel.base.view.TitleNavBarView;
import com.elong.businesstravel.modules.orders.OrdersActivity;

/**
 * 支付宝提交网页
 * 
 * @author lenovo
 * 
 */
public class PaypalWebViewActivity extends BaseWebViewActivity {

	private String mTitleString;
	private String mUrlString;
	private String mReturnUrlString;

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_webview);

		mTitleString = getIntent().getStringExtra("title");
		mUrlString = getIntent().getStringExtra("url");
		mReturnUrlString = getIntent().getStringExtra("returnurl");

		// clearWebViewCache();

		mWebView = (WebView) findViewById(R.id.webView);
		initWebView(mWebView);
		mWebView.getSettings().setCacheMode(WebSettings.LOAD_NO_CACHE);
		DebugLog.logI(mUrlString);
		mWebView.loadUrl(mUrlString);

		setListener();
		setTitleNavBar();
	}

	@Override
	protected void setListener() {
		mWebView.setWebViewClient(new WebViewClient() {
			private boolean flag = false;

			public boolean shouldOverrideUrlLoading(WebView view, String url) {
				if (url.contains(mReturnUrlString)) {
					if (!flag) {
						Intent intent = new Intent(mContext,
								OrdersActivity.class);
						intent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
						startActivity(intent);
						finish();
						flag = true;
					}
				} else {
					view.loadUrl(url);
				}
				return true;
			}

			public void onPageFinished(WebView view, String url) {

			}

			public void onPageStarted(WebView view, String url, Bitmap favicon) {

			}
		});
	}

	@Override
	protected void setTitleNavBar() {

		TitleNavBarView titleNavBarView = (TitleNavBarView) findViewById(R.id.titleNavBarView);
		titleNavBarView.setMessage(mTitleString);
		titleNavBarView.setLeftDrawableImage(R.drawable.btn_title_back);
		titleNavBarView.setLeftButtonImageString(0, "返回", R.color.text_blue);
		titleNavBarView.setLeftButtonOnClickListener(new OnClickListener() {

			@Override
			public void onClick(View v) {
				// 返回
				finish();
			}
		});
		titleNavBarView.setRightButtonVisibility(View.INVISIBLE);
	}

}
